//
//  SearchDayRangeV.swift
//  ANOA
//
//  Created by 邱仙凯 on 2023/2/11.
//  Copyright © 2023 zwb. All rights reserved.
//   左边搜索框 右边日期的固定搜索

import UIKit
class RangeBaseV:UIView,DateWidgetable {
   
    lazy var bg:UIView = {
       let v = UIView()
        v.layer.cornerRadius = 5
        v.backgroundColor = UIColor(kshex: "#E5E8EE")
        return v
    }()
    var searchBar:UISearchBar?
    lazy var titleLab:UILabel = {
       let v = UILabel()
        v.numberOfLines = 0
        v.textAlignment = .center
        v.font = UIFont.systemFont(ofSize: 23)
        return v
    }()
    var linex:UIView = {
        var v = UIView()
        v.backgroundColor = UIColor(kshex: "#CACEE1")
        return v
    }()
    lazy var startBtn:UIButton = {
       let v = UIButton()
        v.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        v.setTitleColor(UIColor(kshex: "#4972F9"), for: .normal)
        return v
    }()
    lazy var endBtn:UIButton = {
       let v = UIButton()
        v.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        v.setTitleColor(UIColor(kshex: "#4972F9"), for: .normal)

        return v
    }()
    lazy var zhiLab:UILabel = {
       let v = UILabel()
        v.numberOfLines = 0
        v.textAlignment = .center
        v.textColor = UIColor(kshex: "#4972F9")
        v.text = "至"
        v.font = UIFont.systemFont(ofSize: 14)
        return v
    }()
    var downImg:UIButton = {
        var v = UIButton()
        v.setBackgroundImage(UIImage(named: "comSearch_down"), for: .normal)
        return v
    }()
    var startD = "" {
        didSet {
            startBtn.setTitle(startD, for: .normal)
         }
    }
    var endD = "" {
        didSet {
            endBtn.setTitle(endD, for: .normal)
         }
    }
    var searchStr = ""
    var startSearchClo:((String,String,String)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        
        self.backgroundColor = .clear
        searchBar = TenderSearchBar()
//        searchController.searchBar.searchTextField.backgroundColor = .white

        searchBar!.searchTextField.backgroundColor = UIColor(kshex: "#E5E8EE")
        searchBar!.searchTextField.font = UIFont.systemFont(ofSize: 14)

            searchBar!.searchTextField.layer.cornerRadius = 8
            searchBar!.searchTextField.layer.masksToBounds = true
     
        searchBar?.backgroundImage = UIImage()
        searchBar!.placeholder = "请输入关键字搜索"
        searchBar?.delegate = self

    
         self.addSubview(bg)
        bg.addSubview(searchBar!)
        bg.addSubview(linex)
        bg.addSubview(startBtn)
        bg.addSubview(zhiLab)
        bg.addSubview(endBtn)
        bg.addSubview(downImg)

        bg.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5))
        }
        searchBar!.snp.makeConstraints { make in
             make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
            make.right.equalTo(linex.snp.left).offset(-2)
             
         }
        linex.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.width.equalTo(1)
            make.height.equalToSuperview().multipliedBy(0.5)
            make.right.equalTo(startBtn.snp.left).offset(-2)
        }
        startBtn.snp.makeConstraints { make in
            make.right.equalTo(zhiLab.snp.left).offset(-2)
            make.centerY.equalToSuperview()
            make.width.equalTo(80)
           make.height.equalTo(28)
           }
        zhiLab.snp.makeConstraints { make in
            make.right.equalTo(endBtn.snp.left).offset(-2)
           make.centerY.equalToSuperview()
           }
        endBtn.snp.makeConstraints { make in
            make.right.equalTo(downImg.snp.left).offset(-2)
            make.centerY.equalToSuperview()

             make.width.equalTo(80)
            make.height.equalTo(28)
           }
        downImg.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-6)
           make.centerY.equalToSuperview()
             make.width.equalTo(10)
            make.height.equalTo(6)
           }
   
      }
     required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    
}
 
extension RangeBaseV: UISearchBarDelegate {

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
    }

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        searchBar.enablesReturnKeyAutomatically = true
    }

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        searchStr = searchBar.text.unwrap
        if let clo = self.startSearchClo {
            clo(self.searchStr,self.startD,self.endD)
        }
    }
}
